import router from "./index";
// 权限列表
export const authList = [
  {
    level: "1",
    authNameList: [
      "Home",
      "AssetsStatistics",
      "AssetsList",
      "UserManage",
      "RoleManage",
      "AuthManage",
      "PersonnelManage",
      "DevManage",
      "DevConfig",
      "DevInfo",
      "DTUManage",
      "DTUList",
      "SIMList",
    ],
  },
  {
    level: "2",
    authNameList: [
      "Home",
      "AssetsStatistics",
      //   "AssetsList",
      "UserManage",
      "RoleManage",
      "AuthManage",
      "PersonnelManage",
      "DevManage",
      "DevConfig",
      "DevInfo",
      "DTUManage",
      "DTUList",
      //   "SIMList",
    ],
  },
];

// 过滤菜单数据的函数
const filterMenuData = (anthNames, routes) => {
  let rRoutes = [];
  routes.forEach((route) => {
    if (anthNames.indexOf(route.name) != -1) {
      rRoutes.push(route);
    }
  });
  return rRoutes;
};

// 登录更新菜单的函数
export const updateMenu = (level) => {
  // 清空本地的菜单数据
  localStorage.removeItem("authMenuList");

  // 对应的权限名称列表
  let levelAuthNameList = authList.filter((item) => item.level == level)[0]
    .authNameList;
  console.log("对应的权限名称列表", levelAuthNameList);
  //   console.log("allRouter", router);
  // 所有菜单列表
  let allMenuList = router.filter((item) => item.name == "Layout")[0].children;

  console.log("所有菜单列表", allMenuList);

  // 处理第一层菜单数据
  let disposeMenuList = filterMenuData(levelAuthNameList, allMenuList);

  // 处理第二层数据
  disposeMenuList.forEach((route2) => {
    if (route2.children) {
      route2.children = filterMenuData(levelAuthNameList, route2.children);

      // 处理第三层数据
      route2.children.forEach((route3) => {
        if (route3.children) {
          route3.children = filterMenuData(levelAuthNameList, route3.children);
        }
      });
    }
  });

  // 将过滤后的权限菜单列表存入本地
  localStorage.setItem("authMenuList", JSON.stringify(disposeMenuList));

  return disposeMenuList;
};
